package org.jeecg.modules.innerApi.leqiApi.convert;

import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.common.util.MathUtil;
import org.jeecg.modules.innerApi.common.requestDto.InvoiceParam;
import org.jeecg.modules.innerApi.leqiApi.requestVo.LQInvoiceFjysParam;
import org.jeecg.modules.innerApi.leqiApi.requestVo.LQInvoiceMxParam;
import org.jeecg.modules.innerApi.leqiApi.requestVo.LQInvoiceParam;
import org.jeecg.modules.xxgx.common.enums.FplxdmEnum;
import org.jeecg.modules.xxgx.common.enums.KplxEnum;
import org.jeecg.modules.xxgx.common.enums.TdyslxDmEnum;
import org.jeecg.modules.xxgx.common.enums.TspzEnum;
import org.jeecg.modules.xxgx.djgl.enums.ZsfsEnum;

import java.util.ArrayList;
import java.util.List;

/**
 * 乐企请求参数转换
 */
public class LQRequestParamConvert {

    /**
     * 发票开具参数转换
     *
     * @param invoiceParam
     * @return
     */
    public static LQInvoiceParam invoiceParamConvert(InvoiceParam invoiceParam) {
        LQInvoiceParam param = new LQInvoiceParam();
        param.setYwqqlsh(invoiceParam.getFpsqlsh());
        param.setYwly("00");//业务来源（发起系统标识）
        param.setYwlx("FPKJ");
        param.setFpdm("");
        param.setFphm("");
        //蓝字发票标志0：蓝字发票  1：红字发票
        String lzfpbz = KplxEnum.ZSFP.getValue().equals(invoiceParam.getKplx())?"0":"1";
        param.setLzfpbz(lzfpbz);
        param.setPtbh(invoiceParam.getKpzddm());//平台编号 取开票终端代码字段
        param.setFppz(invoiceParam.getFplxdm());
        //转换发票类型代码
        if (!FplxdmEnum.sdzp.getValue().equals(invoiceParam.getFplxdm()) && !FplxdmEnum.sdpp.getValue().equals(invoiceParam.getFplxdm())) {
            throw new JeecgBootException("发票类型不支持");
        }

        //特定要素类型转换待处理
        String tdyslxDm = convertTdyslxdm(invoiceParam);
        param.setTdys(tdyslxDm);//特定要素类型代码
        param.setQyDm("14100000000");//区域代码 cwgx todo: 待处理，从哪取值（终端配置）
        param.setCezslxDm(convertCezslxDm(invoiceParam));//差额征税类型代码
        param.setSgfplxDm(convertSgfplxDm(invoiceParam));//收购发票类型代码
        param.setCkywsyzcDm("");//出口业务适用政策代码   待完善
        param.setZzsjzjtDm("");//增值税即征即退代码
        param.setXsfnsrsbh(invoiceParam.getXfsh());
        param.setXsfmc(invoiceParam.getXfmc());
        param.setXsfdz(invoiceParam.getXfdz());
        param.setXsfdh(invoiceParam.getXfdh());
        param.setXsfkhh(invoiceParam.getXfyh());
        param.setXsfzh(invoiceParam.getXfzh());
        param.setGmfnsrsbh(invoiceParam.getKhsh());
        param.setGmfmc(invoiceParam.getKhmc());
        param.setGmfdz(invoiceParam.getKhdz());
        param.setGmfdh(invoiceParam.getKhdh());
        param.setGmfkhh(invoiceParam.getKhyh());
        param.setGmfzh(invoiceParam.getKhzh());
        param.setGmfjbr("");//购买方经办人姓名

        param.setHjje(invoiceParam.getHjje());
        param.setHjse(invoiceParam.getHjse());
        param.setJshj(MathUtil.add(invoiceParam.getHjje(), invoiceParam.getHjse()));//价税合计

        param.setSkyhmc("");//收款银行名称
        param.setSkyhzh("");//收款银行账号
        param.setJsfs("");//结算方式
        param.setYsxwfsd("");//应税行为发生地

        param.setKpr(invoiceParam.getKpr());//开票人
        param.setKprzjhm("");//开票人证件号码
        param.setKprzjlx("");//开票人证件类型

        param.setDylzfphm(invoiceParam.getYfphm());//对应蓝字发票号码
        param.setHzqrxxdbh(invoiceParam.getHzxxbbh());
        param.setHzqrduuid("");//cwgx todo:uuid 数据来源

        param.setBz(invoiceParam.getBz());

        //商品明细
        List<LQInvoiceMxParam> fyxmList = new ArrayList<>();
        invoiceParam.getFpmxs().forEach(djmx -> {
            LQInvoiceMxParam fyxm = new LQInvoiceMxParam();
            fyxm.setMxxh(djmx.getMxhh());
            //fyxm.setDylzfpmxxh("");
            //spmc处理，去掉*前面的分类
            String xmmc = djmx.getSpmc();
            xmmc = xmmc.substring(xmmc.lastIndexOf("*"));
            fyxm.setXmmc(djmx.getSpmc());

            fyxm.setHwhyslwfwmc(djmx.getSpmc());
            fyxm.setSpfwjc(djmx.getSsmc());
            fyxm.setGgxh(djmx.getGgxh());//规格型号
            fyxm.setDw(djmx.getJldw());//单位
            fyxm.setSl(djmx.getSpsl() != null ? djmx.getSpsl().toPlainString() : "");//商品数量
            fyxm.setDj(djmx.getBhsdj());
            fyxm.setJe(djmx.getBhsje());
            fyxm.setSlv(djmx.getTax());
            fyxm.setSe(djmx.getTaxje());
            fyxm.setHsje(djmx.getHsje());
            fyxm.setKce(djmx.getZkje());
            fyxm.setSphfwssflhbbm(djmx.getSpbm());
            fyxm.setFphxz(djmx.getFphxz());
            fyxm.setYhzcbs(LQInvoiceMxParam.yhzcbsEnum.getDescByValue(djmx.getZzstsgl()));//优惠政策标识

            fyxmList.add(fyxm);
        });
        param.setFpmxList(fyxmList);

        //附加要素
        if (invoiceParam.getFjysList() != null && invoiceParam.getFjysList().size() > 0) {
            List<LQInvoiceFjysParam> fjysList = new ArrayList<>();
            invoiceParam.getFjysList().forEach(fjysVO -> {
                LQInvoiceFjysParam fjys = new LQInvoiceFjysParam();
                fjys.setFjyslx(fjysVO.getSjlx());
                fjys.setFjysz(fjysVO.getFjysz());
                fjys.setFjysmc(fjysVO.getXmmc());
                fjysList.add(fjys);
            });
            param.setFjysList(fjysList);
        }
        return param;
    }

    /**
     * 根据系统中特殊票种和特定业务转换为智云接口需要的特定要素类型代码
     *
     * @param invoiceParam
     * @return
     */
    private static String convertTdyslxdm(InvoiceParam invoiceParam) {
        String tdyslxdm = "";
        if (TspzEnum.NCPXS.getValue().equals(invoiceParam.getTspz())) {
            tdyslxdm = LQInvoiceParam.tdysDmEnum.zcncpxsfp.getValue();
        } else if (TspzEnum.NCPSG.getValue().equals(invoiceParam.getTspz())) {
            tdyslxdm = LQInvoiceParam.tdysDmEnum.ncpsgfp.getValue();
        } else if (TspzEnum.ZKTXF.getValue().equals(invoiceParam.getTspz())) {
            tdyslxdm = LQInvoiceParam.tdysDmEnum.txf.getValue();
        } else if (TspzEnum.QTTXF.getValue().equals(invoiceParam.getTspz())) {
            tdyslxdm = LQInvoiceParam.tdysDmEnum.txf.getValue();
        } else if (TspzEnum.CPYXS.getValue().equals(invoiceParam.getTspz())) {
            tdyslxdm = LQInvoiceParam.tdysDmEnum.cpyfp.getValue();
        } else if (TspzEnum.JDC.getValue().equals(invoiceParam.getTspz())) {
            tdyslxdm = LQInvoiceParam.tdysDmEnum.jdc.getValue();
        } else if (TdyslxDmEnum.JZFW.getValue().equals(invoiceParam.getTdyslxdm())) {
            tdyslxdm = LQInvoiceParam.tdysDmEnum.jzfwfp.getValue();
        } else if (TdyslxDmEnum.HWYSFW.getValue().equals(invoiceParam.getTdyslxdm())) {
            tdyslxdm = LQInvoiceParam.tdysDmEnum.hwysfwfp.getValue();
        } else if (TdyslxDmEnum.BDCXS.getValue().equals(invoiceParam.getTdyslxdm())) {
            tdyslxdm = LQInvoiceParam.tdysDmEnum.bdcxsfwfp.getValue();
        } else if (TdyslxDmEnum.BDCJYZLFW.getValue().equals(invoiceParam.getTdyslxdm())) {
            tdyslxdm = LQInvoiceParam.tdysDmEnum.bdcjyzlfw.getValue();
        } else if (TdyslxDmEnum.LKYSFW.getValue().equals(invoiceParam.getTdyslxdm())) {
            tdyslxdm = LQInvoiceParam.tdysDmEnum.lkysfwfp.getValue();
        }
        return tdyslxdm;
    }

    /**
     * 转换差额征税类型代码
     * @param invoiceParam
     * @return
     */
    private static String convertCezslxDm(InvoiceParam invoiceParam) {
        String cezslxdm = "";
        if(ZsfsEnum.CEZS.getValue().equals(invoiceParam.getZsfs())){
            cezslxdm = LQInvoiceParam.cezslxDmEnum.cekp.getValue();
        } else if(ZsfsEnum.QEZS.getValue().equals(invoiceParam.getZsfs())){
            cezslxdm = LQInvoiceParam.cezslxDmEnum.qekp.getValue();
        } else {
            cezslxdm = "";
        }
        return cezslxdm;
    }

    /**
     * 转换收购发票类型代码
     * @param invoiceParam
     * @return
     */
    private static String convertSgfplxDm(InvoiceParam invoiceParam) {
        String sgfplxDm = "";
        if(TspzEnum.NCPSG.getValue().equals(invoiceParam.getTspz())) {
            sgfplxDm = LQInvoiceParam.sgfplxDmEnum.ncpsgfp.getValue();
        } else {//待扩充
            sgfplxDm = "";
        }
        return sgfplxDm;
    }
}
